﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agr.CQRS.Domain;
using Agr.Questionnaire.Core.Domain.Qnnaire.Events;

namespace Agr.Questionnaire.Core.Domain.Qnnaire
{
    public class Questionnaire : AggregateRoot
    {
        internal string Name { get; set; }
        internal IList<QnnaireSection> Sections { get; set; }

        public Questionnaire()
        {
            Sections = new List<QnnaireSection>();
            AddSection(QnnaireSection.DEFAULT_SECTION);
        }

        public void UseName(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ValidationException("Name can't be empty.");

            HandleNewEvent(new QuestionnaireNamedEvent(name));
        }
        private void handleQuestionnaireNamedEvent(QuestionnaireNamedEvent ev)
        {
            Name = ev.Name;
        }

        public void AddSection(string name)
        {
            var s = new QnnaireSection { Name = name };
            Aggregate.RegisterEntity(s);
            Sections.Add(s);
        }

        internal QnnaireSection GetSection(string name)
        {
            if (string.IsNullOrEmpty(name))
                name = QnnaireSection.DEFAULT_SECTION;
            return (from s in Sections where s.Name == name select s).FirstOrDefault();
        }
    }
}
